// 显示分类列表
function showCategoryList() {
  const username = localStorage.getItem('username');
  if (!username) {
    layer.msg('请先登录');
    window.location.href = 'login.html';
    return;
  }

  document.getElementById('categoryManagement').style.display = 'block';
  loadCategories();
}

// 加载分类列表
function loadCategories() {
  const username = localStorage.getItem('username');
  fetch(`/api/category/list?username=${username}`)
    .then(response => response.json())
    .then(result => {
      if (result.code === 1) {
        renderCategoryList(result.data);
      } else {
        layer.msg(result.msg);
      }
    });
}

// 添加分类
function addCategory() {
  const username = localStorage.getItem('username');
  const categoryName = document.getElementById('categoryName').value;

  fetch('/api/category/add', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username: username,
      name: categoryName
    })
  })
    .then(response => response.json())
    .then(result => {
      if (result.code === 1) {
        layer.msg('添加成功');
        loadCategories();
      } else {
        layer.msg(result.msg);
      }
    });
}

// 删除分类
function deleteCategory(categoryId) {
  layer.confirm('确定要删除该分类吗？', function (index) {
    fetch(`/api/category/delete/${categoryId}`, {
      method: 'DELETE'
    })
      .then(response => response.json())
      .then(result => {
        if (result.code === 1) {
          layer.msg('删除成功');
          loadCategories();
        } else {
          layer.msg(result.msg);
        }
      });
    layer.close(index);
  });
} 